package com.eian.boot.module.system.dept.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.eian.boot.common.constants.PermissionConstants;
import com.eian.boot.common.core.exception.ExceptionHelper;
import com.eian.boot.common.core.model.CommonResult;
import com.eian.boot.module.system.associations.user.dept.service.UserDeptRelationService;
import com.eian.boot.module.system.dept.model.dto.DeptCreateRequest;
import com.eian.boot.module.system.dept.model.dto.DeptUpdateRequest;
import com.eian.boot.module.system.dept.model.dto.UserDeptAssignRequest;
import com.eian.boot.module.system.dept.model.vo.DeptTreeVO;
import com.eian.boot.module.system.dept.service.SysDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理控制器
 *
 * @author alex.meng
 */
@Tag(name = "部门管理")
@RestController
@RequestMapping("/system/dept")
@AllArgsConstructor
public class DeptController {

    private final SysDeptService deptService;
    private final UserDeptRelationService userDeptRelationService;

    @Operation(summary = "创建部门")
    @SaCheckPermission(PermissionConstants.Dept.CREATE)
    @PostMapping("/create")
    public CommonResult<String> create(@Valid @RequestBody DeptCreateRequest request) {
        deptService.createDept(request);
        return CommonResult.ok("部门创建成功");
    }

    @Operation(summary = "获取部门树")
    @SaCheckPermission(PermissionConstants.Dept.LIST)
    @GetMapping("/tree")
    public CommonResult<List<DeptTreeVO>> getTree() {
        return CommonResult.ok(deptService.getDeptTree());
    }

    @Operation(summary = "获取部门详情")
    @SaCheckPermission(PermissionConstants.Dept.QUERY)
    @GetMapping("/{id}")
    public CommonResult<DeptTreeVO> getDetail(@Parameter(description = "部门ID") @PathVariable Long id) {
        return CommonResult.ok(deptService.getDeptDetail(id));
    }

    @Operation(summary = "更新部门")
    @SaCheckPermission(PermissionConstants.Dept.UPDATE)
    @PostMapping("/update")
    public CommonResult<String> update(@Valid @RequestBody DeptUpdateRequest request) {
        deptService.updateDept(request);
        return CommonResult.ok("部门更新成功");
    }

    @Operation(summary = "删除部门")
    @SaCheckPermission(PermissionConstants.Dept.DELETE)
    @PostMapping("/delete/{id}")
    public CommonResult<String> delete(@Parameter(description = "部门ID") @PathVariable Long id) {
        deptService.deleteDept(id);
        return CommonResult.ok("部门删除成功");
    }

    @Operation(summary = "分配用户部门")
    @SaCheckPermission(PermissionConstants.User.ASSIGN_DEPT)
    @PostMapping("/assign-user")
    public CommonResult<String> assignUserDept(@Valid @RequestBody UserDeptAssignRequest request) {
        // 验证主部门存在
        ExceptionHelper.ifNull(deptService.getById(request.getMainDeptId()), "主部门不存在");

        // 验证兼职部门存在
        if (request.getPartDeptIds() != null && !request.getPartDeptIds().isEmpty()) {
            for (Long deptId : request.getPartDeptIds()) {
                ExceptionHelper.ifNull(deptService.getById(deptId), "兼职部门不存在：" + deptId);
            }
        }

        // 委托给关系服务处理
        userDeptRelationService.assignUserDept(
                request.getUserId(),
                request.getMainDeptId(),
                request.getPartDeptIds()
        );
        return CommonResult.ok("部门分配成功");
    }
}

